package com.madzone.wiki.gae.ds.model;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Unique;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class User {
	/* jspwiki.userdatabase.table=users
	jspwiki.userdatabase.uid=uid
	jspwiki.userdatabase.email=email
	jspwiki.userdatabase.fullName=full_name
	jspwiki.userdatabase.loginName=login_name
	jspwiki.userdatabase.password=password
	jspwiki.userdatabase.wikiName=wiki_name
	jspwiki.userdatabase.created=created
	jspwiki.userdatabase.modified=modified
	jspwiki.userdatabase.lockExpiry=lock_expiry
	jspwiki.userdatabase.attributes=attributes
	jspwiki.userdatabase.roleTable=roles
	jspwiki.userdatabase.role=role */

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
	@Persistent
	@Unique
	private String email;
	
	@Persistent
	private String uid;
	
	@Persistent
	@Unique
	private String fullName;
	@Persistent
	@Unique
	private String loginName;
	@Persistent
	private String password;
	
	@Persistent
	@Unique
	private String wikiName;
	@Persistent
	private Date created;
	@Persistent
	private Date modified;
	@Persistent
	private Date lockExpiry;
	@Persistent
	private HashMap<String, String> attributes;
	
	@Persistent
	private List<Key> createdGroups;
	
	@Persistent
	private List<Key> modifiedGroups;
	
	@Persistent
	private List<Key> memberOfGroups;
	
	public Key getKey() {
		return key;
	}

	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getFullName() {
		return fullName;
	}
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}
	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getWikiName() {
		return wikiName;
	}
	public void setWikiName(String wikiName) {
		this.wikiName = wikiName;
	}
	public Date getCreated() {
		return created;
	}
	public void setCreated(Date created) {
		this.created = created;
	}
	public Date getModified() {
		return modified;
	}
	public void setModified(Date modified) {
		this.modified = modified;
	}
	public Date getLockExpiry() {
		return lockExpiry;
	}
	public void setLockExpiry(Date lockExpiry) {
		this.lockExpiry = lockExpiry;
	}
	public HashMap<String, String> getAttributes() {
		return attributes;
	}
	public void setAttributes(HashMap<String, String> attributes) {
		this.attributes = attributes;
	}
	
	public static User getByLoginName(String loginName) {
		String query = "select from " + User.class.getName() + " where loginName == '" + loginName +"'";
		PersistenceManager pm = PMF.getPM();
		try {
			List<User> users = (List<User>) pm.newQuery(query).execute();
			if (users.size() == 1) {
				return users.get(0);
			}
			else if (users.size() == 0) {
				return null;
			}
			else {
				//No way that we get more than one user
				throw new RuntimeException("There are more than one user for username " + loginName);
			}
		} finally {
			//pm.close();
		}
	}
	public static User getByEmailId(String emailId) {
		String query = "select from " + User.class.getName() + " where email == '" + emailId +"'";
		PersistenceManager pm = PMF.getPM();
		try {
			List<User> users = (List<User>) pm.newQuery(query).execute();
			if (users.size() == 1) {
				return users.get(0);
			}
			else if (users.size() == 0) {
				//No such user
				return null;
			}
			else {
				//No way that we get more than one user
				throw new RuntimeException("There are more than one user for email id " + emailId);
			}
		} finally {
			//pm.close();
		}
	}
	public static User getByFullName(String fullName) {
		String query = "select from " + User.class.getName() + " where fullName == '" + fullName +"'";
		PersistenceManager pm = PMF.getPM();
		try {
			List<User> users = (List<User>) pm.newQuery(query).execute();
			if (users.size() == 1) {
				return users.get(0);
			}
			else if (users.size() == 0) {
				//No such user
				return null;
			}
			else {
				//No way that we get more than one user
				throw new RuntimeException("There are more than one user for full name " + fullName);
			}
		} finally {
			//pm.close();
		}
	}
	public static User getByWikiName(String wikiName) {
		String query = "select from " + User.class.getName() + " where wikiName == '" + wikiName +"'";
		PersistenceManager pm = PMF.getPM();
		try {
			List<User> users = (List<User>) pm.newQuery(query).execute();
			if (users.size() == 1) {
				return users.get(0);
			}
			else if (users.size() == 0) {
				//No such user
				return null;
			}
			else {
				//No way that we get more than one user
				throw new RuntimeException("There are more than one user for wikiName " + wikiName);
			}
		} finally {
			//pm.close();
		}
	}
	public static User getByUid(String uid) {
		String query = "select from " + User.class.getName() + " where uid == '" + uid +"'";
		PersistenceManager pm = PMF.getPM();
		try {
			List<User> users = (List<User>) pm.newQuery(query).execute();
			if (users.size() == 1) {
				return users.get(0);
			}
			else if (users.size() == 0) {
				return null;
			}
			else {
				//No way that we get more than one user
				throw new RuntimeException("There are more than one user for uid " + uid);
			}
		} finally {
			//pm.close();
		}
	}
	public static List<User> getAllUsers() {
		String query = "select from " + User.class.getName();
		PersistenceManager pm = PMF.getPM();
		try {
			List<User> users = (List<User>) pm.newQuery(query).execute();
			return users;
		} finally {
			//pm.close();
		}
	}
	public void save() {
		PersistenceManager pm = PMF.getPM();
		try {
			pm.makePersistent(this);
		} finally {
			//pm.close();
		}
	}
	
	public void delete() {
		PersistenceManager pm = PMF.getPM();
		try {
			pm.deletePersistent(this);
		} finally {
			//pm.close();
		}
	}
}
